
/* JavaScript content from js/driver-book-find.js in folder common */
var myBusyIndicator = new WL.BusyIndicator('main',{text : "waiting..."});

/**
 * initialize the book list
 */
function initializePage() {
//	var invocationData = {
//			adapter : 'stAdp',
//			procedure : 'getHistoryBook',
//			parameters : [{'dri_id': window.driverInfo.id}]
//	};
//	myBusyIndicator.show();
//	WL.Client.invokeProcedure(invocationData, {
//		onSuccess : getHistoryBookSuccess,
//		onFailure : getHistoryBookFailure
//	});
	findBookSuccess({});
}

/**
 * get find book success, initialize the page according to the result
 */
function findBookSuccess(result) {
//	myBusyIndicator.hide();
	var testData = [ {
		'order_id' : '1',
		'fromAddr' : 'shanghaizhangjiang',
		'cus_id' : '1',
		'nickname' : 'Zheng MimgFeng',
		'credit' : 3
	}, {
		'order_id' : '2',
		'fromAddr' : 'shanghai',
		'cus_id' : '4',
		'nickname' : 'Samu Lu',
		'credit' : 5
	}, {
		'order_id' : '3',
		'fromAddr' : 'zhangjiang',
		'cus_id' : '5',
		'nickname' : 'Bai YuShi',
		'credit' : 2
	},
	{
		'order_id' : '1',
		'fromAddr' : 'shanghaizhangjiang',
		'cus_id' : '1',
		'nickname' : 'Zheng MimgFeng',
		'credit' : 3
	}, {
		'order_id' : '2',
		'fromAddr' : 'shanghai',
		'cus_id' : '4',
		'nickname' : 'Samu Lu',
		'credit' : 5
	}, {
		'order_id' : '3',
		'fromAddr' : 'zhangjiang',
		'cus_id' : '5',
		'nickname' : 'Bai YuShi',
		'credit' : 2
	},
	{
		'order_id' : '1',
		'fromAddr' : 'shanghaizhangjiang',
		'finishTime' : '08/21/2013 14:00',
		'cus_id' : '1',
		'nickname' : 'Zheng MimgFeng',
		'credit' : 3
	}, {
		'order_id' : '2',
		'fromAddr' : 'shanghai',
		'cus_id' : '4',
		'nickname' : 'Samu Lu',
		'credit' : 5
	}, {
		'order_id' : '3',
		'fromAddr' : 'zhangjiang',
		'cus_id' : '5',
		'nickname' : 'Bai YuShi',
		'credit' : 2
	}];
	$.each(testData, function(i, item) {
		var itemContent = "<div class='bookItem'><div style='float:left'><div class='leftCommon boldFont'>";
		itemContent = itemContent.concat(item.fromAddr);
		itemContent = itemContent.concat("</div><p style='clear: left'></p>");
		itemContent = itemContent.concat("<div class='leftCommon smallFont'>" + item.nickname + "</div>");
		itemContent = itemContent.concat("<div class='leftCommon'>");
		for(var j = 0; j < item.credit; j++) {
			itemContent = itemContent.concat("<img src='../images/star.png' width='14px' height='14px'>");
		}
		itemContent = itemContent.concat("</div></div><div style='float:right'><a href='#' onclick='acceptBook(" + item.order_id + ","+ item.cus_id + ")'><img src='../images/acceptBtn.png' width='40px' height='40px'></a></div><p style='clear: both'></p></div>");
		$(itemContent).appendTo($("#findBookList"));
	});
	
}

/**
 * after the driver click the btn of accept the order
 * @param order_id
 */
function acceptBook(order_id, cus_id) {
//	var invocationData = {
//			adapter : 'stAdp',
//			procedure : 'getHistoryBook',
//			parameters : [{'dri_id': window.driverInfo.id, 'order_id': order_id, 'cus_id': cus_id}]
//	};
//	myBusyIndicator.show();
//	WL.Client.invokeProcedure(invocationData, {
//		onSuccess : acceptBookSuccess,
//		onFailure : acceptBookFailure
//	});
	alert(order_id + "" + cus_id);
	acceptBookSuccess({'status':0});
	this.innerHTML = "";
}

/**
 * accept success
 */
function acceptBookSuccess(result) {
//	myBusyIndicator.hide();
	if(result.status == 0) {
//		window.currentElement.html("");
	}else if(result.status == 1){
		$(this).html("accept by someone else");
	}else {
		$(this).html("accept fail");
	}
}

/**
 * get history book fail, remind the user
 */
function getHistoryBookFailure() {
	alert("getHistoryBookFailure");
}
(function($){
	$(".backBtn").on('tap', function() {
		$.mobile.changePage("driver-main.html");
	});
	initializePage();
})(jQuery);